diff --git a/swh/web/common/swh_templatetags.py b/swh/web/common/swh_templatetags.py index 0cdcbee71..43ca39411 100644 --- a/swh/web/common/swh_templatetags.py +++ b/swh/web/common/swh_templatetags.py @@ -1,92 +1,82 @@ # Copyright (C) 2017 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import re from docutils.core import publish_parts from docutils.writers.html4css1 import Writer, HTMLTranslator from inspect import cleandoc from django import template -from django.utils.safestring import mark_safe - -from pygments import highlight -from pygments.lexers import JsonLexer -from pygments.formatters import HtmlFormatter register = template.Library() class NoHeaderHTMLTranslator(HTMLTranslator): """ Docutils translator subclass to customize the generation of HTML from reST-formatted docstrings """ def __init__(self, document): super().__init__(document) self.body_prefix = [] self.body_suffix = [] def visit_bullet_list(self, node): self.context.append((self.compact_simple, self.compact_p)) self.compact_p = None self.compact_simple = self.is_compactable(node) self.body.append(self.starttag(node, 'ul', CLASS='docstring')) DOCSTRING_WRITER = Writer() DOCSTRING_WRITER.translator_class = NoHeaderHTMLTranslator @register.filter def safe_docstring_display(docstring): """ Utility function to htmlize reST-formatted documentation in browsable api. """ docstring = cleandoc(docstring) return publish_parts(docstring, writer=DOCSTRING_WRITER)['html_body'] @register.filter def urlize_api_links(text): """Utility function for decorating api links in browsable api. Args: text: whose content matching links should be transformed into contextual API or Browse html links. Returns The text transformed if any link is found. The text as is otherwise. """ text = re.sub(r'(/api/[^"<]*/|/browse/.*/|http.*$)', r'\1', text) return re.sub(r'([^ <>"]+@[^ <>"]+)', r'\1', text) @register.filter def urlize_header_links(text): """Utility function for decorating headers links in browsable api. Args text: Text whose content contains Link header value Returns: The text transformed with html link if any link is found. The text as is otherwise. """ return re.sub(r'<(/api/.*|/browse/.*)>', r'<\1>', text) - - -@register.filter -def highlight_json(text): - return mark_safe(highlight(text, JsonLexer(), HtmlFormatter())) diff --git a/swh/web/templates/apidoc.html b/swh/web/templates/apidoc.html index 07cd69e60..55d9ae11b 100644 --- a/swh/web/templates/apidoc.html +++ b/swh/web/templates/apidoc.html @@ -1,139 +1,149 @@ {% extends "layout.html" %} {% load swh_templatetags %} +{% load static %} {% block title %}{{ heading }} – Software Heritage API {% endblock %} +{% block header %} + + + +{% endblock %} {% block content %} {% if docstring %}

Description

{{ docstring | safe_docstring_display | safe }}
{% endif %} {% if response_data %}

Request

{{ request.method }} {{ request.build_absolute_uri }}

Response

{% if status_code != 200 %}

Status Code

{{ status_code }}
{% endif %} {% if headers_data %}

Headers

{% for header_name, header_value in headers_data.items %}
{{ header_name }} {{ header_value | urlize_header_links | safe }}
{% endfor %} {% endif %}

Body

- {{ response_data | highlight_json | urlize_api_links | safe }} +
+      {{ response_data | urlize_api_links | safe }}
+    
{% endif %}
{% if urls and urls|length > 0 %}
{% for url in urls %} {% endfor %}
URL Allowed Methods
{{ url.rule }} {{ url.methods | dictsort:0 | join:', ' }}

{% endif %} {% if args and args|length > 0 %}

Arguments

{% for arg in args %}
{{ arg.name }}: {{ arg.type }}
{{ arg.doc | safe_docstring_display | safe }}
{% endfor %}

{% endif %} {% if params and params|length > 0 %}

Parameters

{% for param in params %}
{{ param.name }}: {{ param.type }}
{{ param.doc | safe_docstring_display | safe }}
{% endfor %}

{% endif %} {% if headers and headers|length > 0 %}

Headers

{% for header in headers %}
{{ header.name }}: string
{{ header.doc | safe_docstring_display | safe }}
{% endfor %}

{% endif %} {% if returns and returns|length > 0 %}

Returns

{% for return in returns %}
{{ return.type }}
{{ return.doc | safe_docstring_display | safe }}
{% endfor %}

{% endif %} {% if excs and excs|length > 0 %}

Errors

{% for exc in excs %}
{{ exc.exc }}
{{ exc.doc | safe_docstring_display | safe }}
{% endfor %}

{% endif %} {% if examples and examples|length > 0 %}

Examples

{% for example in examples %}
{{ example }}
{% endfor %}
{% endif %} {% endblock %} diff --git a/swh/web/templates/content.html b/swh/web/templates/content.html index de06bd960..51cfd14e0 100644 --- a/swh/web/templates/content.html +++ b/swh/web/templates/content.html @@ -1,29 +1,48 @@ {% extends "layout.html" %} +{% load static %} + +{% block header %} + + + + +{% endblock %} + {% block title %}Content display{% endblock %} {% block content %}
Content mime type: {{ mime_type }} , Hashing algorithm: {{ content_hash_algo}} , Content checksum: {{ content_checksum }} Raw File
{% include "content-directory-top-navigation.html" %}
{% if "inode/x-empty" == mime_type %} File is empty {% elif "text/" in mime_type %}
     {{ content }}
   
{% elif "image/" in mime_type and content %} {% else %} Content with mime type {{ mime_type }} can not be displayed {% endif %}
{% endblock %} diff --git a/swh/web/templates/layout.html b/swh/web/templates/layout.html index bea313451..0ea0a6fd8 100644 --- a/swh/web/templates/layout.html +++ b/swh/web/templates/layout.html @@ -1,90 +1,79 @@ {% load static %} {% block title %}{% endblock %} + + {% block header %}{% endblock %} + - - - -
{% block content %}{% endblock %}
back to top